<?php
namespace app\admin\logic;
use app\admin\validate\ClassLearn as ClassLearnValidate;
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class ClassLearnLogic extends BaseLogic {

    /**
     * 检查修改学习规则
     * @param $params
     * @return mixed
     */
    public function check_save_cl_rule($params){
        $validate = new ClassLearnValidate();
        $r = $validate->scene('edit')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }
        $num = array_sum($params['ckid']);
        if($params['ti_num'] != $num){
            return $this->get_response(0,'题目总数应等于题库总数之和');
        }
        return $this->get_response(1,'OK');
    }

    /**
     * 获取学习统计查询条件
     * @param $department
     * @param $username
     * @return array
     */
    public function get_search_user_maps($department,$username){
        $maps = [];
        $maps[] = ['role','>',1];
        $maps[] = ['status','=',1];

        if($department != '全部'){
            $maps[] = ['department','=',$department];
        }

        if(!empty($username)){
            $maps[] = ['username','like','%' . $username . '%'];
        }
        return $maps;
    }

    /**
     * 获取学习记录查询条件
     * @param $clid
     * @param $openid
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function get_search_clr_maps($clid,$openid,$start_time,$end_time){
        $maps = [];
        $maps[] = ['clid','=',$clid];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['end_time','>','0000-00-00 00:00:00'];

        if(!empty($start_time) && !empty($end_time)){
            $maps[] = ['start_time','between',[$start_time,$end_time]];
        }

        return $maps;
    }

    /**
     * 计算学习统计
     * @param $clid
     * @param $openid
     * @param $start_time
     * @param $end_time
     * @return mixed
     */
    public function count_cl_total($clid,$openid,$start_time,$end_time){
        $maps = $this->get_search_clr_maps($clid,$openid,$start_time,$end_time);

        $total_num = model('class_learn_ci')->get_count($maps,'id');

        $right_num = model('class_learn_ci')->get_sum($maps,'right_num');

        $wrong_num = model('class_learn_ci')->get_sum($maps,'wrong_num');

        $data['total_num'] = $total_num;
        $data['right_num'] = $right_num;
        $data['wrong_num'] = $wrong_num;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 获取学习统计
     * @param $params
     * @param $clid
     * @return mixed
     */
    public function get_cl_total($params,$clid){
        $maps = $this->get_search_user_maps($params['department'],$params['username']);
        $limit = ($params['page'] -1) * $params['rows'];

        $list = model('user')->get_list($maps,'id,openid,username,department','id asc');
        foreach($list as $k => $v){
            //获取学习统计
            $r = $this->count_cl_total($clid,$v['openid'],$params['start_time'],$params['end_time']);
            $list[$k]['total_num'] = $r['data']['total_num'];
            $list[$k]['right_num'] = $r['data']['right_num'];
            $list[$k]['wrong_num'] = $r['data']['wrong_num'];
        }

        $list = $list->toArray();
        $total_nums = array_column($list,'total_num');
        array_multisort($total_nums,SORT_DESC,$list);

        $list = $params['rows'] != 0 ? array_splice($list,$limit,$params['rows']) : $list;

        $total_num = model('user')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 生成学习统计excel
     * @param $ExcelName
     * @param $data
     * @return bool
     */
    public function gen_cl_total_excel($ExcelName,$data){
        if(count($data[0]) > 26){
            return $this->get_response(0,'导出excel最大列数为26，请缩减数据列数');
        }
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        for($row = 1;$row <= count($data);$row++){
            $index = 0;
            foreach($data[$row-1] as $k => $v) {
                $col = chr($index + 65);
                $cell = ($col . $row);
                $sheet->setCellValue($cell, $v);
                $index++;
            }
        }

        header('Content-Type: application/vnd.ms-excel;charset=utf8-general-ci');
        header('Content-Disposition: attachment;filename='.$ExcelName);
        header('Cache-Control: max-age=0');

        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');
        //$writer->save($ExcelName);
        return true;
    }
}